package com.model.manyToMany;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "TB_TEACHER")
public class Teacher {

	@Id
	@GenericGenerator(name = "generator", strategy = "increment")
	@GeneratedValue(generator = "generator")
	@Column(name = "ID", unique = true, nullable = false)
	private int id;
	
	@Column(name = "NO", unique = true, nullable = false, length = 30)
	private String no;
	
	@Column(name = "NAME", nullable = false, length = 30)
	private String name;
	
	@Column(name = "COURSE", nullable = false, length = 30)
	private String course;
	
	@ManyToMany
    @JoinTable(
        name="TB_TEACHER_STUDENT",
        joinColumns=@JoinColumn(name="TEA_ID"),
        inverseJoinColumns=@JoinColumn(name="STU_ID")
    )
    @Cascade(value = { CascadeType.SAVE_UPDATE })
    //两表及中间表全部删除，cascade=ALL
    //只删除一表及中间表,或只删除中间表，cascade!=DELETE
	private List<Student> students = new ArrayList<Student>();

	public int getId() {
		return id;
	}

	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCourse() {
		return course;
	}

	public void setCourse(String course) {
		this.course = course;
	}

	public List<Student> getStudents() {
		return students;
	}

	public void setStudents(List<Student> students) {
		this.students = students;
	}

}
